package com.easygoods.controller;

import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.exception.AssertUtils;
import com.bestvike.stone.spring.page.PageableResponse;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.request.OrderCancelRequest;
import com.easygoods.request.OrderCommitRequest;
import com.easygoods.request.OrderListRequest;
import com.easygoods.request.OrderRefundRequest;
import com.easygoods.request.OrderSendRequest;
import com.easygoods.request.OrderSignRequest;
import com.easygoods.request.OrderTraceRequest;
import com.easygoods.request.OrderTrailRequest;
import com.easygoods.response.OrderCommitResponse;
import com.easygoods.response.OrderDetailResponse;
import com.easygoods.response.OrderListResponse;
import com.easygoods.response.OrderTraceResponse;
import com.easygoods.response.OrderTrailResponse;
import com.easygoods.service.OrderService;
import com.easygoods.service.OrderTraceService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhq
 * @date 2019/1/2 15:44
 */
@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderTraceService orderTraceService;

    //订单试算
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/trail")
    public CommonResponse<OrderTrailResponse> trailOrder(@RequestBody OrderTrailRequest request) {
        final OrderTrailResponse response = this.orderService.trailOrder(request);
        return CommonResponse.success(response);
    }

    //提交订单
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/commit")
    public CommonResponse<OrderCommitResponse> commitOrder(@RequestBody OrderCommitRequest request) {
        OrderCommitResponse response = this.orderService.commitOrder(request);
        return CommonResponse.success(response);
    }

    //发货
    @RequiresPermissions(UserPermissions.MANAGE)
    @PostMapping("/send")
    public CommonResponse<Object> sendOrder(@RequestBody OrderSendRequest request) {
        this.orderService.sendOrder(request);
        return CommonResponse.success();
    }

    //取消订单 (只有待支付的才能取消)
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/cancel")
    public CommonResponse<Object> cancelOrder(@RequestBody OrderCancelRequest request) {
        this.orderService.cancelOrder(request.getOrderId());
        return CommonResponse.success();
    }

    //订单签收
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/sign")
    public CommonResponse<Object> signOrder(@RequestBody OrderSignRequest request) {
        this.orderService.signOrder(request.getOrderId());
        return CommonResponse.success();
    }

    //申请退款
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/refund")
    public CommonResponse<Object> refundOrder(@RequestBody OrderRefundRequest request) {
        this.orderService.refundOrderRequest(request.getOrderId(), request.getReason(), request.getRefundFileList());
        return CommonResponse.success();
    }

    //物流跟踪
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/trace")
    public CommonResponse<OrderTraceResponse> getOrderTraces(@RequestBody OrderTraceRequest request) {
        OrderTraceResponse orderTraces = this.orderTraceService.getOrderTraces(request.getOrderId());
        return CommonResponse.success(orderTraces);
    }

    //查询订单列表
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/list")
    public CommonResponse<PageableResponse<OrderListResponse>> getOrderList(@RequestBody OrderListRequest orderListRequest) {
        PageableResponse<OrderListResponse> orderInfoList = this.orderService.getOrderList(orderListRequest);
        return CommonResponse.success(orderInfoList);
    }

    //查询订单详情
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/detail")
    public CommonResponse<OrderDetailResponse> getOrderDetail(String orderId) {
        AssertUtils.hasText(orderId, "orderId");
        final OrderDetailResponse orderDetail = this.orderService.getOrderDetail(orderId);
        return CommonResponse.success(orderDetail);
    }
}
